<!--# layout("/common/layout.html",{"jsBase":"/js/plandata/tDPlanningData/"}){ -->
<div class="layui-row">
    <form class="layui-form layui-form-pane" id="planForm"  lay-filter="editform">
        <div class="layui-collapse" lay-filter="condictionCol">
            <div class="layui-form-item">
                <h2 class="layui-colla-title">筛选条件</h2>
                <div class="layui-colla-content layui-show">
                    <div class="layui-row">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label" >年份</label>
                                <div class="layui-input-inline"  id="yearDiv" >
                                    <select  class="select" name="year" id="year" lay-filter="yearselect">
                                        <option value="2018" selected>2018</option>
                                        <option value="2017">2017</option>
                                    </select>
                                </div>
                                <label class="layui-form-label" >文理科</label>
                                <div class="layui-input-inline" >
                                    <select  class="select" name="type" id="type" lay-filter="typeselect">
                                        <option value="1" selected>文科</option>
                                        <option value="2">理科</option>
                                    </select>
                                </div>
                                <label class="layui-form-label" >省份</label>
                                <div class="layui-input-inline"  id="provIdDiv" >
                                    <select  class="select" name="ProvinceId" id="provId" url="${ctxPath}/plandata/tDPlanningData/getProvinceList.json"
                                             lay-filter="myselect"   selectValue="" promtion="请选择省" lay-search >
                                        <option value="">请选择省</option>
                                    </select>
<!--
                                    <select style="width:40%" class="select" name="ProvinceId" id="provId" selectValue="" promtion="请选择" lay-search lay-filter="myselect">
                                        <option value="">请选择</option>
                                    </select>-->
                                </div>

                            </div>
                        </div>
                    </div>
                    <div class="layui-row">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label" >批次</label>
                                <div class="layui-input-inline">
                                    <select style="width:40%" class="select" name="batch" id="batch" selectValue="" promtion="请选择" lay-search lay-filter="batchselect">
                                        <option value="">请选择</option>
                                    </select>
                                </div>

                                <label class="layui-form-label">学校</label>
                                <div class="layui-input-inline" id="cityDiv" >
                                    <select style="width:40%" class="select" name="univ" id="univ" selectValue="" promtion="请选择" lay-search lay-filter="univselect">
                                        <option value="">请选择</option>
                                    </select>
                                </div>
                                <button style="float: right;" class="layui-btn layui-btn-normal" type="button" id="planSearch">搜索</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<br/>
<form class="layui-form layui-form-pane" id="addForm"  lay-filter="editform">
    <input type="hidden" name="provinceId" id="provinceId" >
    <input type="hidden" name="majorTypeId" id="majorTypeId">
    <input type="hidden" name="year" id="planYear">
    <input type="hidden" name="batchName" id="batchName">
    <input type="hidden" name="umId" id="umId" >
    <input type="hidden" name="univId" id="univId" >
    <input type="hidden" name="majorCount" id="majorCount">
    <input type="hidden" name="batchId" id="batchId">

    <div class="layui-row">
        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">学校名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="univName" id="univName" autocomplete="off"  class="layui-input" >
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">学校代码</label>
                <div class="layui-input-inline">
                    <input type="text" name="univCode" id="univCode" autocomplete="off" class="layui-input" >
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label" style="width: 150px">计划招生人数</label>
                <div class="layui-input-inline">
                    <input type="text" name="plan" id="plan" autocomplete="off" class="layui-input" >
                </div>
            </div>

        </div>
    </div>

    <div class="layui-row">
        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label" >学校测评</label>
                <div class="layui-input-block" style="width:512px">
                    <textarea name="univEvaluation"  id="univEvaluation" placeholder="请输入" class="layui-textarea" ></textarea>
                </div>


            </div>
        </div>
    </div>

    <label class="layui-form-label">专业列表</label>

    <div id="addDiv">
        <button type="button" class="layui-btn layui-btn-sm" data-type="addRow" id="addRow" style="height: 38px" title="添加一行">
            <i class="layui-icon layui-icon-add-1"></i> 添加一行
        </button>
    </div>

    <div class="layui-row">
      <table id="dataTable" lay-filter="dataTable"></table>
    </div>

    <layui:submitButtons id="planAdd" />
</form>
<!--#} -->
<script>
layui.extend({
    loading: '../../../plugins/layui_extends/loading/loading',
    cascadeSelect: '../../../plugins/layui_extends/cascadeSelect'  //  {/}的意思即代表采用自有路径，即不跟随 base 路径
}).define([ 'form', 'laydate', 'table','loading','tDPlanningDataApi','cascadeSelect'], function(exports) {
    var form = layui.form;
    var cascadeSelect = layui.cascadeSelect;
    var index = layui.index;
    var table = layui.table;
    var tableIns ;
    var isNew = true;
    $("#addDiv").hide();
   cascadeSelect.init('ProvinceId', true);
    // $("label").css("margin-left","65px")
    // setProv();
    var view = {
        init:function(){
            Lib.initGenrealForm($("#planForm"),form);
            this.initSubmit();
        },
        initSubmit:function(){
            $("#planAdd").click(function(){
                // $("#addButton").hide();
                // $("#addButton-loading").show();
                // $("#addButton-loading").loading();
                form.on('submit(form)', function(){
                    // tDPlanningDataApi.saveForm(isNew,$('#planForm'),function(){
                    //     // parent.window.dataReload();
                    //     // Common.info("保存成功");
                    //     Lib.closeFrame();
                    // });
                   var data =  $("#addForm").serializeJson();
                   var majorData =table.cache['dataTable']
                   data.marjors=JSON.stringify(majorData);
                   var planCount = 0;
                    $.each(majorData, function(key, val) {
                        planCount += val.plan;
                    });
                    if(data.umId==null||data.umId==""){
                        Common.info("请去新增数据");
                        return ;
                    }

                    if(data.plan< planCount){
                        Common.info("每个专业招生人数总和不能大于计划招生人数");
                        return ;
                    }
                    $.ajax({
                        type: 'POST',
                        url: '${ctxPath}/plandata/tDPlanningData/addPlanData.json',
                        data: data,
                        async: false,
                        dataType:'json',
                        success: function(data){
                            if(data.code == 0){
                                Common.info("保存成功");
                            }else{
                                Common.info(data.msg);
                            }
                            Lib.closeFrame();
                        }
                    });
                });
            });

            $("#planAdd-cancel").click(function(){
                Lib.closeFrame();
            });

            $("#planSearch").click(function(){
                var provinceId = $('#provId option:selected').val();
                var year = parseInt($('#year option:selected').val());
                var type = $('#type option:selected').val();
                var umId = $('#univ option:selected').val();
                var batchId = $('#batch option:selected').val();

                $("#provinceId").val(provinceId);
                $("#majorTypeId").val(type);
                $("#planYear").val(year+1);
                $("#batchId").val(batchId)
                $("#batchName").val($('#batch option:selected').text());

                if(provinceId==null||provinceId==""){
                    Common.info("请选择筛选条件");
                    return;
                }
                var  result  = new Array();
                result = getMajor(provinceId,year,type,umId,batchId);
                $("#addDiv").show();
                tableIns = table.render({
                    elem: '#dataTable'
                    ,height: 'full'
                   // ,url: '${ctxPath}/plandata/tDPlanningData/getMajor.json?year='+year+'&type='+type+'&provinceId='+provinceId+'&univId='+univId+'&batchId='+batchId //数据接口
                    ,data: result
                    ,page: false //开启分页
                    ,limit:100
                    ,cols: [[ //表头
                        {field: 'majorName', title: '专业名称',edit: 'text'}
                        ,{field: 'majorCode', title: '专业代码', edit: 'text',width: 100}
                        ,{field: 'plan', title: '招生人数' ,edit: 'number',width: 100}
                        ,{field: 'majorEvaluation', title: '专业测评',edit: 'text'}
                        ,{field: 'remark', title: '备注',edit: 'text'}
                        ,{field: 'cost', title: '学费(元)',edit: 'text'}
                        ,{field: 'schoolyear', title: '年限',edit: 'number'}
                        ,{field: '', title: '操作',
                            templet: function(d){
                                return '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" lay-id=""><i class="layui-icon layui-icon-delete"></i>移除</a>';
                            }
                        }
                    ]]
                    ,done:function (res, curr, count) {
                        if(count==0){
                            $("#addDiv").hide();
                        }
                    }
                });
            });
        }

    }

    $(document).on('click','#addRow',function(obj){
        var oldData = table.cache['dataTable'];
        if(oldData==null||oldData==''){
            oldData = [];
        }
        var newRow = {
            majorName:null,
            majorCode:null,
            plan:1,
            majorEvaluation:null,
            remark:null,
            cost:null,
            schoolyear:null
        };

        oldData.push(newRow);

        $("#majorCount").val(oldData.length);
        tableIns.reload({
            data : oldData
        });
    });

    //监听工具条
    table.on('tool(dataTable)', function (obj) {
        var  data = obj.data,event = obj.event; //获得当前行 tr 的DOM对象;
        switch(event){
            case "del":
                layer.confirm('确认删除该行？', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);
                    var oldData = table.cache['dataTable']
                    oldData.splice(obj.tr.data('index'),1);
                    $("#majorCount").val(oldData.length);
                    tableIns.reload({
                        data : oldData
                    });
                });
                break;

        }
    });

    view.init();

    function clear(){
        $("#provId").val("");
        setBatch();
        form.render('select');
    }

    form.on('select(yearselect)', function(data){
        clear();
    });
    form.on('select(typeselect)', function(data){
        clear();
    });

    //省份选择事件监听
    form.on('select(myselect)', function(data){
        setBatch(data.value);
    });

    //批次选择事件监听
    form.on('select(batchselect)', function(data){
        setUniv(data.value);
    });

    function getMajor(provinceId,year,type,umId,batchId) {
        var res;
        $.ajax({
            type: 'POST',
            url: '${ctxPath}/plandata/tDPlanningData/getMajor.json',
            data: {
                provinceId:provinceId,
                year:year,
                type:type,
                umId:umId,
                batchId:batchId
            },
            async: false,
            dataType:'json',
            success: function(data){
                setValue(data.data.plan);
                res= data.data.major;
            }
        });
        return res;
    }

    function setValue(data) {
        console.log(data);
        $.each(data,function(key,val){
            $("#univCode").val(val.univCode);
            $("#plan").val(val.plan);

            $("#majorCount").val(val.majorCount);

            $("#univName").val(val.univName);
            $("#univEvaluation").val(val.univEvaluation);

            $("#umId").val(val.umId);
            $("#univId").val(val.univId);
        })
    }

    function setBatch(id) {
        var year = parseInt($('#year option:selected').val())+1;
        var type = $('#type option:selected').val();
        $.ajax({
            type: 'POST',
            url: "${ctxPath}/plandata/tDPlanningData/getBatch.json",
            data: {
                id:id,
                year:year,
                type:type
            },
            dataType:'json',
            success: function(data){
                $("#batch").html("");
                $.each(data, function(key, val) {
                    var option1 = $("<option>").val(val.value).text(val.key);
                    $("#batch").append(option1);
                });
                form.render('select');
                var batchId = $('#batch option:selected').val();
                setUniv(batchId);
            }
        });
    }

    function setUniv(id) {
        var year = $('#year option:selected').val();
        var type = $('#type option:selected').val();
        var provinceId = $('#provId option:selected').val();
        $.ajax({
            type: 'POST',
            url: "${ctxPath}/plandata/tDPlanningData/getBaseUniv.json",
            data: {
                batchId:id,
                year:year,
                type:type,
                provinceId:provinceId
            },
            dataType:'json',
            success: function(data){
                $("#univ").html("");
                // var option1 = $("<option>").val("").text("请选择");
                // $("#univ").append(option1);
                $.each(data, function(key, val) {
                    var option1 = $("<option>").val(val.value).text(val.key);
                    $("#univ").append(option1);
                });
                form.render('select');
            }
        });
    }

});
</script>
